package cn.tedu.sims.ex.handler;

import cn.tedu.sims.ex.ServiceException;
import cn.tedu.sims.web.JsonResult;
import cn.tedu.sims.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.nio.file.AccessDeniedException;
import java.nio.file.FileSystemException;
import java.util.Set;
import java.util.StringJoiner;

/**
 * 处理异常抛出
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult<Void> handlerServiceException(ServiceException e){
        log.debug("处理了ServiceException异常");
        return JsonResult.fail(e);
    }

    @ExceptionHandler
    public JsonResult<Void> handlerBindException(BindException e){
        log.debug("处理了BindException异常");
        String message=e.getFieldError().getDefaultMessage();
        return JsonResult.fail(ServiceCode.ERR_BAD_REQUEST,message);
    }

    @ExceptionHandler
    public JsonResult<Void> handleConstraintViolationException(ConstraintViolationException c){
        log.debug("处理了ConstraintViolationException异常");
        /*String message=c.getMessage().substring(c.getMessage().lastIndexOf(" "));*/
        StringJoiner stringJoiner=new StringJoiner(".");
        Set<ConstraintViolation<?>> set=c.getConstraintViolations();
        for (ConstraintViolation<?> constraintViolation :set){
            stringJoiner.add(constraintViolation.getMessage());
        }
        return JsonResult.fail(ServiceCode.ERR_BAD_REQUEST,stringJoiner.toString());
    }

    @ExceptionHandler({
            InternalAuthenticationServiceException.class, // AuthenticationServiceException >>> AuthenticationException
            BadCredentialsException.class // AuthenticationException
    })
    public JsonResult<Void> handleAuthenticationException(AuthenticationException e) {
        log.debug("捕获到AuthenticationException");
        log.debug("异常类型：{}", e.getClass().getName());
        log.debug("异常消息：{}", e.getMessage());
        String message = "登录失败，用户名或密码错！";
        return JsonResult.fail(ServiceCode.ERR_UNAUTHORIZED, message);
    }

    @ExceptionHandler
    public JsonResult<Void> handleDisabledException(DisabledException e) {
        log.debug("捕获到DisabledException");
        String message = "登录失败，此账号已经被禁用！";
        return JsonResult.fail(ServiceCode.ERR_UNAUTHORIZED_DISABLED, message);
    }

    @ExceptionHandler
    public String handleThrowable(Throwable e) {
        String message = "你有异常没有处理，请根据服务器端控制台的信息，补充对此类异常的处理！！！";
        e.printStackTrace();
        return message;
    }

    @ExceptionHandler(
            AccessDeniedException.class
    )
    public JsonResult<Void> AccessDeniedException(FileSystemException e){
        return JsonResult.fail(ServiceCode.ERR_POWER,"权限不足");
    }

}
